﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mapenect.Data;
using System.Runtime.InteropServices;
using OpenNI;
using Mapenect.GesturePatterns;
using System.Diagnostics;

namespace Mapenect.GestureInterpreter
{
    class PreInterpreter1 : Interpreter
    {
        [DllImport("kernel32.dll")]
        public static extern bool Beep(int Frequenz, int Dauer);

        public PreInterpreter1(DataContainer data, float thresh)
            : base(data, null, thresh)
        {
            this.GestureDetected += new GestureEventhandler(AfterGestureDetected);
        }

        protected override bool checkForGesture()
        {
            //Get Joints
            MyJoint rHand = jointData.getUserData(user.user1)[SkeletonJoint.RightHand].GetLastElement();
            MyJoint rElbow = jointData.getUserData(user.user1)[SkeletonJoint.RightElbow].GetLastElement();
            MyJoint rShoulder = jointData.getUserData(user.user1)[SkeletonJoint.RightShoulder].GetLastElement();

            //Calculate Tolerance-Zone
            float tolerance = rShoulder.Y * threshold;
            float minY = rShoulder.Y - tolerance;
            float maxY = rShoulder.Y + tolerance;

            //Create Vecors
            MyVector shoulderToElbow = new MyVector(rShoulder, rElbow);
            MyVector elbowToHand = new MyVector(rElbow, rHand);
            
            MyVector shNorm = shoulderToElbow.Normalize();
            MyVector elNorm = elbowToHand.Normalize();

            Debug.WriteLine(shNorm.GetString());
            Debug.WriteLine(elNorm.GetString());
            Debug.WriteLine(shoulderToElbow.CompareTo(elbowToHand, threshold) ? "true" : "false");

            if( (rHand.Y > minY && rHand.Y < maxY) &&
                (rElbow.Y > minY && rElbow.Y < maxY) 
                //&& (rShoulder.X < rElbow.X && rElbow.X < rHand.X)
                //&& shoulderToElbow.CompareTo(elbowToHand, threshold)
                )
            {
                return true;
            }
            else
            {
                return false;
            }   
        }

        private void AfterGestureDetected(object sender, EventArgs e)
        {
            Beep(1000, 1000);
        }
    }
}
